<template>
  <div class="base-container" id="main">
    <div style="display: flex;font-weight: bold;color: #000000;justify-content: center;font-size: 29px;padding: 20px">
      江西省地方标准征求意见处理汇总表
    </div>
    <div
      style="display: flex;font-weight: bold;color: #000000;justify-content: center;font-size: 21px;margin-bottom: 10px;">
      （试行）
    </div>
  <t-form labelAlign="top" ref="form" :data="formData" :rules="rules" class="step-form table-form"
          labelWidth="140px">
<!--   <div style="display: flex;width: 100%;align-items: center;margin-bottom: 10px"><div>起草单位（盖章）：</div><div>{{ draftUnitId | filterByTree(deptMenuOptions) }}</div><div style="margin-left: 20px"><span style="color: red;-->
<!--      margin-right: 3px;">*</span>汇总日期：</div><div><t-form-item name="collectDate"><t-date-picker style="width: 390px" clearable v-model="formData.collectDate"></t-date-picker>-->
<!--   </t-form-item></div></div>-->
    <t-row>
      <t-col :span="2" class="title"><span class="require">*</span>汇总日期</t-col>
      <t-col :span="10">
        <t-form-item name="collectDate">
          <t-date-picker style="width: 390px" clearable v-model="formData.collectDate"></t-date-picker>
        </t-form-item>
      </t-col>
    </t-row>
    <t-row>
      <t-col :span="2" class="title"><span class="require">*</span>标准计划号</t-col>
      <t-col :span="4">
        <t-form-item name="planNum">
          <t-input clearable v-model="formData.planNum"></t-input>
        </t-form-item>
      </t-col>
      <t-col :span="2" class="title">标准名称</t-col>
      <t-col :span="4">
        <t-form-item name="projectName">
         {{ formData.projectName }}
        </t-form-item>
      </t-col>
    </t-row>
    <t-row>
      <t-col :span="2" class="title"><span class="require">*</span>技术归口单位</t-col>
      <t-col :span="10">
        标准委员会
      </t-col>
    </t-row>
    <t-row>
      <t-col :span="2" class="title"><span class="require">*</span>意见分发和回收情况</t-col>
      <t-col :span="10" style="flex-direction: column">
        <t-row style="width: 100%">
          <t-col class="sub-title text-center" :flex="1">单位</t-col>
          <t-col class="sub-title text-center" :flex="1">政府部门</t-col>
          <t-col class="sub-title text-center" :flex="1">行业协会</t-col>
          <t-col class="sub-title text-center" :flex="1">企业</t-col>
          <t-col class="sub-title text-center" :flex="1">科研机构</t-col>
          <t-col class="sub-title text-center" :flex="1">检验检测机构</t-col>
          <t-col class="sub-title text-center" :flex="1">认证认可机构</t-col>
          <t-col class="sub-title text-center" :flex="1">消费者代表</t-col>
        </t-row>
        <t-row style="width: 100%">
          <t-col class="text-center" :flex="1" >发出份数</t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.zfbmFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.hyxhFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.qyFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.kyjgFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.jyjcjgFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.rzrkjgFc' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.xfzdbFc' :min="0"></t-input-number></t-col>
        </t-row>
        <t-row style="width: 100%">
          <t-col class="text-center" :flex="1">收回份数</t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.zfbmSh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.hyxhSh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.qySh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.kyjgSh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.jyjcjgSh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.rzrkjgSh' :min="0"></t-input-number></t-col>
          <t-col class="text-center" :flex="1"><t-input-number :allowInputOverLimit="false" v-model='formData.xfzdbSh' :min="0"></t-input-number></t-col>
        </t-row>
      </t-col>
    </t-row>
    <t-row>
      <t-col class="text-center" style="font-weight: bold" :span="12">意见及处理情况</t-col>
    </t-row>
    <t-row>
      <t-col :span="12">
        <t-form-item name="handleSituations">
        <opinion-fixed-list ref="opinion-fixed-list" :edit-open="true" v-model="formData.handleSituations"></opinion-fixed-list>
        </t-form-item>
      </t-col>
    </t-row>
    <t-row>
      <t-col :span="2" class="title">备注</t-col>
      <t-col :span="10">
        <div>1.标准内容不涉及的单位类别，发出份数、收回份数可填“0”；</div>
        <div>2.意见及处理情况：所有意见均需填写。</div>
      </t-col>
    </t-row>
  </t-form>
    <t-row style="margin-top: 20px;text-align: center">
      <t-col :span="12">
        <t-space :size="10">
          <t-button theme="primary" :loading="applyLoading" @click="onSubmit(1)">{{
              applyLoading ? '提交中...' : '暂存'
            }}
          </t-button>
          <t-button theme="primary" :loading="applyLoading" @click="onSubmit(2)">{{
              applyLoading ? '提交中...' : '提交'
            }}
          </t-button>
        </t-space>
      </t-col>
    </t-row>
  </div>
</template>

<script>
import opinionFixedList from "./components/opinion-fixed-list.vue";

export default {
  name: "opinion-apply",
  components:{
    opinionFixedList
  },
  data() {
    return {
      applyLoading: false,
      formData: {
        localId: null,
        collectDate: '',
        planNum: '',
        projectName: '',
        zfbmFc: 0,
        zfbmSh: 0,
        hyxhFc: 0,
        hyxhSh: 0,
        qyFc: 0,
        qySh: 0,
        kyjgFc: 0,
        kyjgSh: 0,
        jyjcjgFc: 0,
        jyjcjgSh: 0,
        rzrkjgFc: 0,
        rzrkjgSh: 0,
        xfzdbFc: 0,
        xfzdbSh: 0,
        handleSituations: []
      },
      rules: {
        handleSituations: [{required: true, validator: (val) => val && val.length, message: "意见及处理情况不能为空", type: "error"}],
      },
      draftUnitId: null,
      deptMenuOptions:[]
    }
  },
  created() {
    const taskId = this.$route.params && this.$route.params.taskId;
    if (!taskId) {
      this.$message.error("缺少id");
      this.close();
      return false;
    }
    this.formData.localId = taskId;
    // 先获取申报表详情
    this.$api.bzh.opinion.getBySuggestId(taskId).then(response => {
      if (!response.data) {
        return false;
      }
      this.formData = Object.assign({}, this.formData, response.data)
    }).catch(e => {
      this.$message.error(e.toString())
    })
    this.$api.bzh.standard.getApplyBySuggestId(taskId).then(res => {
      if (!res.data) {
        return false;
      }
      this.formData.projectName = res.data.projectName;
      this.draftUnitId = res.data.draftUnits[0].unitId;
    }).catch(e => {
      this.$message.error(e.toString())
    })
    this.getDeptTreeSelect();
  },
  methods:{
    getDeptTreeSelect() {
      return this.$api.system.dept.treeSelectAllIn().then(response => {
        this.deptMenuOptions = response.data;
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    onSubmit(type) {
      if (type === 1) {
        this.applyLoading = true;
        if (!this.formData.id) {
          this.$api.bzh.opinion.add(this.formData).then(response => {
            this.formData.id = response.data.id;
            this.applyLoading = false;
            this.$message.success('暂存成功');
          }).catch((e) => {
            this.applyLoading = false;
            this.$message.error(e.toString());
          });
        } else {
          this.$api.bzh.opinion.update(this.formData).then(response => {
            this.applyLoading = false;
            this.$message.success('暂存成功');
          }).catch((e) => {
            this.applyLoading = false;
            this.$message.error(e.toString());
          });
        }
      } else {
        this.$refs.form.validate().then((validateResult) => {
          if (validateResult === true) {
            const isValidate = this.$refs["opinion-fixed-list"].checkData()
            if (!isValidate) {
              return false
            }
            const confirmDia = this.$dialog.confirm({
              header: "提交后不可再编辑，会自动进入到下一阶段",
              body: `是否确认提交？`,
              theme: "warning",
              onConfirm: ({e}) => {
                confirmDia.update({
                  confirmBtn: {
                    loading: true,
                  },
                })
                this.applyLoading = true;
                this.$api.bzh.opinion.submit(this.formData).then(response => {
                  this.applyLoading = false;
                  confirmDia.destroy();
                  this.$message.success('提交成功');
                  this.close();
                }).catch((e) => {
                  this.applyLoading = false;
                  confirmDia.update({
                    confirmBtn: {
                      loading: false,
                    },
                  })
                  this.$message.error(e.toString());
                });
              },
            })



          } else {
            const firstError = Object.values(validateResult)[0]?.[0]?.message;
            this.$message.error(firstError);
          }
        });
      }
    },
    close() {
      this.$tab.closeOpenPage({path: "/bzh/opinion/opinion-my"});
    }
  }
}
</script>

<style scoped>

</style>
